var app = getApp();
var offset;
var drole;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clientslist: [],
    clientsLoaded: false,
    num1: 0,
    num2: 0,
    roleFlag: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      clientsLoaded: false
    });
    app.ready(() => {
      offset = 0;
      drole = 0;
      this.getAllCount();
    });
  },
  loadList: function(pageoffset, drole) {
    app.showLoading();
    var url = '/api/Distribut/userList';
    var params = {
      page: pageoffset,
      drole: drole
    }
    app.request(url, params).then(this.loadListCallBack.bind(this));
  },
  loadListCallBack: function(res) {
    app.hideLoading();
    var tem_arr = [];
    var role = {};
    if (res.status == 'ok') {
      if (offset > 0) {
        tem_arr = this.data.clientslist;
      }
      for (var i = 0; i < res.result.role.length; i++) {
        role[res.result.role[i].level_id] = res.result.role[i].level_name;
      };
      for (var i = 0; i < res.result.ulist.arr.length; i++) {
        if (!res.result.ulist.arr[i].head_pic) {
          res.result.ulist.arr[i].head_pic = '../../imgs/demoheadicon.png';
        }
        tem_arr.push(res.result.ulist.arr[i]);
      }
    }
    this.setData({
      clientslist: tem_arr,
      role: role,
      clientsLoaded: true
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    offset++;
    this.loadList(offset);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.shareToIndex();
  },
  getClients: function(e) {
    app.showLoading();
    drole = e.currentTarget.dataset.role;
    this.setData({
      roleFlag: drole
    });
    offset = 0;
    this.loadList(offset, drole);
  },
  getAllCount: function() {
    var url = '/api/Distribut/userList';
    var param1 = {
      page: 0,
      drole: 0
    };
    var param2 = {
      page: 0,
      drole: 1
    }
    Promise.all([
      app.request(url, param1),
      app.request(url, param2)
    ]).then(res => {
      var num1 = res[0].result.ulist.num ? res[0].result.ulist.num : 0;
      var num2 = res[1].result.ulist.num ? res[1].result.ulist.num : 0;
      this.setData({
        num1: num1,
        num2: num2
      })
      this.loadListCallBack(res[0]);
    });
  }
})